#! /bin/bash
#数据库自动备份脚本
# 定义时间
# filename=$(date +%Y%m%d)
declare curr_datetime=date;
# 取年月日
declare CUR_YYYYMD_HMS="$(${curr_datetime} "+%Y%m%d")";

# 合并后，得到 年月日
declare DATETIME=$CUR_YYYYMD_HMS;
echo ${DATETIME};

#要备份的数据库
declare DB_NAME=main;

#备份路径
declare PATH_PRE=/home/data/bak/mysql;
declare PATH_DB_NAME=${DB_NAME}_${DATETIME}.sql;
declare PATH_TAR_NAME=${DB_NAME}_${DATETIME}.tar.gz;

echo "cd ${PATH_PRE}"
cd ${PATH_PRE}
echo "dump start...";
echo "    ->: ${PATH_DB_NAME}";
/usr/local/mysql/bin/mysqldump  -usa -P3306  -p123  -h192.168.1.254 --single-transaction   ${DB_NAME} >> ${PATH_DB_NAME};
echo "dump end.";
echo "";
echo "tar start...";
echo " ${PATH_PRE}:   ${PATH_DB_NAME} -> ${PATH_TAR_NAME}";
tar -czf ${PATH_TAR_NAME} ${PATH_DB_NAME};
rm -rf ${PATH_DB_NAME};
#删除10天前备份
find ${PATH_PRE} -mtime +10 -name '*.tar.gz' -exec rm -rf {} \;
echo "tar end.";
